package com.alex.gridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

import com.alex.ui.MGridView;
import com.alex.ui.ass.GridViewAttr;
import com.alex.ui.ass.ViewWithShadowAttr;
import com.helios.ui.IView;

public class GridViewActivity extends Activity {
	
	private IView mView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		MGridView viewGrid = (MGridView) findViewById(R.id.activity_main_gridview);
		IView mViewFocus = (IView) findViewById(R.id.activity_main_focus);
		mView = viewGrid;
//		设置Adapter
		viewGrid.setAdapter(new GridAdapter(getApplicationContext()));

		ViewWithShadowAttr focusAttr = new ViewWithShadowAttr();
		focusAttr.width = 0;
		focusAttr.height = 0;
		focusAttr.paddingLeft = 28;
		focusAttr.paddingTop = 28;
		focusAttr.paddingRight = 28;
		focusAttr.paddingBottom = 28;
//		viewGrid.setSingleFocusView(new FocusView(getApplicationContext()), focusAttr);
//		设置GridView的focusView
		viewGrid.setGlobalFocusView(mViewFocus, focusAttr, 100, 100);

		GridViewAttr attrGrid = new GridViewAttr();
		attrGrid.paddingLeft = 140;
		attrGrid.paddingTop = 140;
		attrGrid.paddingRight = 140;
		attrGrid.paddingBottom = 140;
		attrGrid.columns = 4;
		attrGrid.columnGap = 40;
		attrGrid.rowGap = 40;
		
		ViewWithShadowAttr attrItem = new ViewWithShadowAttr();
		attrItem.width = 200;
		attrItem.height = 200;
		attrItem.paddingLeft = 10;
		attrItem.paddingTop = 10;
		attrItem.paddingRight = 10;
		attrItem.paddingBottom = 10;
		attrItem.hasFocusedScale = true;
		attrItem.scalePrivotX = 10;
		attrItem.scalePrivotY = 10;
		attrItem.scaleX = 1.16;
		attrItem.scaleY = 1.16;
//		设置GridView和item的LayoutParams参数
		viewGrid.setGridViewAttr(attrGrid, attrItem);
		
		viewGrid.setSelected(49);
		viewGrid.setMFocus(true);
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (mView.hasMFocus() && mView.dispatchKeyEvent(event)) {
			return true;
		} else {
			if (KeyEvent.ACTION_DOWN==event.getAction() 
					&& KeyEvent.KEYCODE_MENU==event.getKeyCode()) {
				mView.setMFocus(!mView.hasMFocus());
			}
		}

		return super.dispatchKeyEvent(event);
	}

}
